<template>
  <div class="add-wrap" v-loading="loading">
    <Breadcrumb :titles="titles"></Breadcrumb>
    <div class="body">
      <div class="body-left">
        <div class="project">
          <el-form
            :model="basicForm"
            label-position="top"
            :rules="basicRules"
            ref="basicFormRef"
          >
            <!-- 基本信息 -->
            <div class="project-part">
              <div
                class="project-part-title project-part-title1"
              >
                基本信息
              </div>
              <div class="project-part-content">
                <el-row :gutter="24">
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="平台名称"
                      prop="platformName"
                    >
                      <el-input
                        v-model="basicForm.platformName"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item label="责任单位">
                      <el-input
                        v-model="basicForm.zrUnit"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="投资额（万元）"
                      prop="investmentAmount"
                    >
                      <el-input
                        v-model="basicForm.investmentAmount"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="项目类型"
                      prop="otherProjectType"
                    >
                      <el-select
                        placeholder=""
                        v-model="basicForm.otherProjectType"
                        clearable
                        :disabled="isDetail"
                      >
                        <el-option
                          v-for="item in otherProjectTypeOptions"
                          :key="item.dictValue"
                          :label="item.dictText"
                          :value="item.dictValue"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                    v-if="showZSFX"
                  >
                    <el-form-item label="中试方向">
                      <el-input
                        v-model="basicForm.zsfx"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                    v-if="showMSXD"
                  >
                    <el-form-item
                      label="岷山行动"
                      prop="msxd"
                    >
                      <el-input
                        v-model="basicForm.msxd"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="上会时间"
                      prop="shTime"
                    >
                      <el-date-picker
                        v-model="basicForm.shTime"
                        type="date"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item label="签约时间">
                      <el-date-picker
                        v-model="basicForm.qyTime"
                        type="date"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="联系人"
                      prop="contactPerson"
                    >
                      <el-input
                        v-model="basicForm.contactPerson"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="联系方式"
                      prop="contactInformation"
                    >
                      <el-input
                        v-model="
                          basicForm.contactInformation
                        "
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item
                      label="项目内容"
                      prop="projectContent"
                    >
                      <el-input
                        style="width: 100%"
                        type="textarea"
                        v-model="basicForm.projectContent"
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>

            <!-- 项目情况 -->
            <div class="project-part">
              <div
                class="project-part-title project-part-title1"
              >
                项目情况
              </div>
              <div class="project-part-content">
                <el-row :gutter="24">
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item label="进展情况">
                      <el-input
                        type="textarea"
                        v-model="
                          basicForm.projectSituation.jzqk
                        "
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item label="存在问题">
                      <el-input
                        type="textarea"
                        v-model="
                          basicForm.projectSituation.czwt
                        "
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item label="下一步建议">
                      <el-input
                        type="textarea"
                        v-model="
                          basicForm.projectSituation.xybjy
                        "
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="isEdit ? 12 : 8"
                    :xl="isEdit ? 8 : 6"
                  >
                    <el-form-item label="备注">
                      <el-input
                        type="textarea"
                        v-model="
                          basicForm.projectSituation.bz
                        "
                        :readonly="isDetail"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-form>
        </div>
      </div>
      <div class="body-right" v-if="isEdit">
        <div class="project-part-title">进度历史记录</div>
        <el-scrollbar>
          <div
            v-for="(item, index) in historyList"
            :key="index"
          >
            <div class="time" @click="clickTime(index)">
              <div
                :style="
                  item.expand ? { color: '#00BAFF' } : ''
                "
              >
                {{ item.createTime }}
              </div>
              <div
                :style="
                  item.expand ? { color: '#00BAFF' } : ''
                "
                v-if="item.expand"
                class="el-icon-arrow-up"
              ></div>
              <div v-else class="el-icon-arrow-right"></div>
            </div>

            <div v-if="item.expand" class="item">
              <div class="item-title">【进展情况】</div>
              <div class="item-text">{{ item.jzqk }}</div>
              <div class="item-title">【存在问题】</div>
              <div class="item-text">{{ item.czwt }}</div>
              <div class="item-title">【下一步建议】</div>
              <div class="item-text">
                {{ item.xybjy }}
              </div>
              <div class="item-title">【备注】</div>
              <div class="item-text">{{ item.bz }}</div>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div class="line" v-show="!isDetail">
      <el-button @click="saveInfo" class="save-btn"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import Breadcrumb from '@/components/breadcrumb'
import {
  addProjectInfo,
  queryProjectDetail
} from '@/api/innovation'

import { getDictOptions } from '@/utils/dict'

export default {
  props: {
    pkid: String,
    title: String,
    isEdit: {
      type: Boolean,
      default: false
    },
    isDetail: {
      type: Boolean,
      default: false
    }
  },
  components: {
    Breadcrumb
  },
  data() {
    return {
      basicForm: {
        platformName: '',
        otherProjectType: '',
        shTime: '',
        qyTime: '',
        contactPerson: '',
        contactInformation: '',
        investmentAmount: '',
        zsfx: '',
        zrUnit: '',
        projectContent: '',
        msxd: '',
        projectSituation: {
          jzqk: '',
          czwt: '',
          xybjy: '',
          bz: ''
        }
      },
      basicRules: {
        platformName: [
          {
            required: true,
            message: '请输入平台名称',
            trigger: 'blur'
          }
        ],
        investmentAmount: [
          {
            pattern:
              /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/,
            message: '金额数字保留两位数字',
            trigger: 'blur'
          }
        ],
        contactInformation: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback()
              }
              const phoneReg =
                /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
              const telReg =
                /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$/
              if (
                !phoneReg.test(value) &&
                !telReg.test(value)
              ) {
                if (!phoneReg.test(value)) {
                  callback(new Error('请输入正确手机号'))
                } else {
                  callback(new Error('请输入正确的座机号'))
                }
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      otherProjectTypeOptions: [],
      historyList: [],
      currentItem: 0,
      loading: false,

      showZSFX: false,
      showMSXD: false
    }
  },
  computed: {
    titles() {
      return [this.title]
    }
  },
  watch: {
    'basicForm.otherProjectType': function (val) {
      if (val === 'zsfx') {
        this.showZSFX = true
        this.showMSXD = false
        this.basicForm.msxd = ''
      } else if (val === 'msxd') {
        this.showZSFX = false
        this.showMSXD = true
        this.basicForm.zsfx = ''
      } else {
        this.showZSFX = false
        this.showMSXD = false
        this.basicForm.zsfx = ''
        this.basicForm.msxd = ''
      }
    }
  },
  mounted() {
    this.queryTypeOptions()
    if (this.isEdit) {
      this.getDetailInfo()
    }
  },
  methods: {
    // 项目分类
    async queryTypeOptions() {
      this.otherProjectTypeOptions = await getDictOptions(
        'zhkc_xmgl_xmlx'
      )
    },

    // 获取项目详情
    getDetailInfo() {
      this.loading = true
      queryProjectDetail(this.pkid).then((res) => {
        if (res.success) {
          const data = res.data
          this.basicForm = data
          this.basicForm.projectSituation =
            data.projectSituationList[0]
          this.historyList = data.projectSituationList.map(
            (item, index) => {
              return {
                ...item,
                expand: index === 0
              }
            }
          )
        }
        this.loading = false
      })
    },

    // 保存信息
    saveInfo() {
      this.$refs.basicFormRef.validate((res) => {
        if (res) {
          addProjectInfo(this.basicForm).then((res) => {
            if (res.success) {
              this.$message.success('保存成功')
              if (this.isEdit) {
                this.getDetailInfo()
              } else {
                this.$router.push({
                  path: '/wisdomInnovation/projectManagement/list',
                  query: { refresh: Date.now() }
                })
              }
            }
          })
        }
      })
    },

    // 点击更新时间
    clickTime(index) {
      this.historyList[index].expand =
        !this.historyList[index].expand
    }
  }
}
</script>

<style lang="scss" scoped>
.add-wrap {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background-color: #fff;
  .body {
    width: 100%;
    display: flex;
    flex: 1;
    overflow: hidden;
    &-left {
      flex: 1;
      overflow: auto;
      height: 100%;
      box-sizing: border-box;
      padding: 20px 10px 20px 10px;
    }

    &-right {
      width: 420px;
      overflow-y: auto;
      border-left: 1px solid #f1f1f1;
      padding: 20px;
      box-sizing: border-box;
      height: 100%;

      .time {
        display: flex;
        justify-content: space-between;
        cursor: pointer;
        margin-bottom: 20px;
        font-size: 14px;

        :hover {
          color: #00baff;
        }
      }

      .item {
        width: 100%;
        padding: 5px 15px 5px 2px;
        box-sizing: border-box;
        border-left: 2px solid #ebf1f8;
        margin-bottom: 20px;

        &-title {
          margin-bottom: 12px;
          font-size: 14px;
        }

        &-text {
          color: #4c565c;
          // text-indent: 10px;
          font-size: 14px;
          margin-bottom: 17px;
          margin-left: 8px;
          &:last-child {
            margin-bottom: 0px;
          }
        }
      }
    }
  }

  .project {
    flex: 1 1 auto;
    overflow: auto;
    &-part {
      margin-top: 10px;

      &-title {
        color: #4c565c;
        line-height: 18px;
        font-size: 16px;
        font-weight: 500;
        margin-bottom: 24px;
        position: relative;
        box-sizing: border-box;
        padding: 0 0 0 11px;
        line-height: 16px;
        &::before {
          content: '';
          width: 4px;
          height: 16px;
          background: linear-gradient(
            180deg,
            #40ebff,
            #bcf8ff
          );
          position: absolute;
          top: 0;
          left: 0;
        }
      }
      &-title1 {
        margin-left: 10px;
      }

      &-content {
        padding-left: 10px;
      }
    }
  }

  ::v-deep {
    .el-form-item__label {
      line-height: 14px !important;
    }
    .el-form-item__content {
      line-height: auto !important;
    }
    .el-input__inner {
      // background: #f6f9fc !important;
      color: #4c565c !important;
      height: 32px !important;
      width: 320px !important;
      // margin-left: 10px;

      .el-input__icon {
        width: 16px !important;
        height: 16px !important;
        font-size: 16px !important;
        line-height: 40px !important;
        margin-top: -25px !important;
      }

      .el-range-separator {
        width: 20px !important;
        margin-top: -5px !important;
      }

      .el-range-input {
        // background: #f6f9fc !important;
      }
    }

    .el-input.is-disabled .el-input__inner {
      background: #e8eff7 !important;
    }

    .el-date-editor.el-input {
      width: auto !important;
    }
    .el-button.is-disabled {
      background: #c0c4cc !important;
      border-color: #c0c4cc !important;
    }

    .el-input-group__append {
      position: absolute;
      height: 20px;
      line-height: 20px;
      width: 80px;
      text-align: right;
      top: 4px;
      left: 235px;
      border-color: rgba(0, 0, 0, 0);
      background: rgba(0, 0, 0, 0);
    }

    .el-input__suffix {
      top: 1px !important;
      right: 8px !important;

      .el-input__icon {
        width: 16px !important;
        height: 16px !important;
        font-size: 16px !important;
        line-height: 30px;
      }
      .is-reverse {
        line-height: 15px !important;
      }
    }

    .el-select {
      width: 320px !important;

      .el-input__inner {
        width: 320px !important;
        // margin-left: 10px;
      }

      .el-input__suffix {
        // top: 4px !important;
        .el-input__icon {
          width: 16px !important;
          height: 16px !important;
          font-size: 16px !important;
          line-height: 15px;
        }
        .is-reverse {
          line-height: 35px !important;
        }
      }

      .is-focus {
        .el-input__suffix {
          top: -3px !important;
        }
      }
    }

    .el-textarea__inner {
      width: 320px !important;
      // background: #f6f9fc !important;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
    }

    .el-breadcrumb__inner {
      color: #606266 !important;
      font-weight: 400 !important;
    }
  }

  .line {
    width: 100%;
    height: 78px;
    background: #fff;
    border-top: 1px solid #f1f1f1;
    box-sizing: border-box;
    padding: 0 30px;
  }

  .save-btn {
    float: right;
    right: 20px;
    background: #497fa3;
    border: 1px solid #497fa3;
    width: 104px;
    color: white;
    font-size: 14px;
    margin-top: 20px;
    width: 104px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
